package com.xiaobai.config;

import com.xiaobai.service.addition.HealthReportService_v2;
import com.xiaobai.service.addition.HealthReportService_v3;
import com.xiaobai.service.addition.IFreeClassroomService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@Slf4j
@Order(9999)
public class Config extends WebMvcConfigurationSupport implements ApplicationRunner {

    @Autowired
    private MyInterceptor myInterceptor;

    @Autowired
    private SchedulingTask schedulingTask;

    @Autowired
    private IFreeClassroomService freeClassroomService;

    @Value("${version}")
    private String version;

    private static final String DEBUG = "debug";

    @Value("${IMAGE_PATH}")
    public void setImagePath(String imagePath) {
        log.info("图片保存路径:{}", imagePath);
        HealthReportService_v2.IMAGES_PATH = imagePath;
        HealthReportService_v3.IMAGES_PATH = imagePath;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/wxFirstLogin","/error","/favicon.ico/error");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // DEBUG版本下边不加载
        if (version.equals(DEBUG)) {
            return;
        }
        Date nowDate = new Date();
        // 每日打卡
        schedulingTask.updateArticle();
        // 初始化代理IP
//        if (!IpProxyUtil.updateItem()) {
//            throw new RuntimeException("代理IP获取失败");
//        }
        // 空闲教室
        // 今天和[+1, +2, +3, +4, +5, +6, +7, +8]
        // 学期最后一天的时间戳  2022-07-09 14:26:39
        {
            long lastDay = 1657347999000L;
            List<Date> list = new ArrayList<>(8);
            for (int i = 1; i <= 7; i++) {
                Date date = DateUtils.addDays(nowDate, i);
                if (date.getTime() > lastDay) {
                    break;
                }
                // 文件不存在才去生成
                if (freeClassroomService.exist(date)) {
                    log.info("{}文件已存在", DateFormatUtils.format(date, "yyyy-MM-dd"));
                    continue;
                }
                list.add(date);
            }
            Date[] dateArr = new Date[list.size()];
            for (int i = 0; i < list.size(); i++) {
                dateArr[i] = list.get(i);
            }
            freeClassroomService.save(dateArr);
        }

    }
}
